package com.heima.es.test;


import com.heima.es.pojo.Goods;
import com.heima.es.repository.GoodsRepository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.es.test
 * @Author: wangjian
 * @CreateTime: 2022-07-10  23:51
 * @Description: TODO
 * @Version: 1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataEsManager {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @Autowired
    private GoodsRepository goodsRepository;

    @Test // 保存和批量保存
    public void testAddGoods(){

        List<Goods> list = new ArrayList<>();
        list.add(new Goods(1L, "小米手机7",  "小米", "手机",3299.00));
        list.add(new Goods(2L, "坚果手机R1",  "锤子","手机", 3699.00));
        list.add(new Goods(3L, "华为META10", "华为","手机", 4499.00));
        list.add(new Goods(4L, "小米Mix2S",  "小米","手机", 4299.00));
        list.add(new Goods(5L, "荣耀V10",  "华为", "手机",2799.00));
        List<Goods> goodsList =  goodsRepository.findByTitleOrBrand("手机","小米");
        System.out.println(goodsList);

        goodsRepository.saveAll(list);

    }

    @Test //更新，就是用保存方法
    public void testUpdateGoods(){
        Goods goods = new Goods(1L,"小米12 PRO","小米","手机",3000.0);
        goodsRepository.save(goods);
    }

    @Test //根据id删除，删除所有
    public void testDeleteGoods(){
        goodsRepository.deleteById(1L);
        goodsRepository.deleteAll();
    }

    @Test //根据id查询 查询所有  分页查询
    public void testQueryGoods(){
        Optional<Goods> goodsOptional = goodsRepository.findById(1L);
        Goods goods = goodsOptional.get();
        System.out.println(goods);

        System.out.println("==================================");
        Iterable<Goods> goodsIterable = goodsRepository.findAll();
        for (Goods goods1 : goodsIterable) {
            System.out.println(goods1);
        }
        System.out.println("------------------------------------");
        Pageable pageable = PageRequest.of(0,3, Sort.by(Sort.Direction.DESC,"price"));
        Page<Goods> goodsPage = goodsRepository.findAll(pageable);
        List<Goods> content = goodsPage.getContent();
        for (Goods goods1 : content) {
            System.out.println(goods1);
        }


    }

    @Test
    public void testRepositoryAndRestClient(){
        QueryBuilder query = QueryBuilders.matchQuery("title","小米");
        Pageable pageable = PageRequest.of(0,3,Sort.by(Sort.Direction.DESC,"price"));
        Iterable<Goods> goodsIterable = goodsRepository.search(query,pageable);
        for (Goods goods : goodsIterable) {
            System.out.println(goods);
        }
    }
}
